package com.jy.appmarket.network.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import android.content.Context;

import com.jy.appmarket.network.engine.cachestrategy.WidgetCacheStrategy;
import com.jy.appmarket.network.resultdata.WidgetResultData;
import com.jy.appmarket.utils.IOTools;
import com.jy.appmarket.utils.LogUtil;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2014年8月27日
 * @version 1.0
 */

public class WidgetNetEngine extends BaseNetEngine {
	private static final String TAG = "WidgetNetEngine";
	public static String TYPE_TOOL = "tool_widget";
	public static String TYPE_GAME = "game_widget";
	public static String TYPE_ENTERTAINMENT = "entertainment_widget";
	private String mCommand;
	private WidgetCacheStrategy mCacheStrategy;
	private boolean mCacheExpired;
	private String mtype;
	public WidgetNetEngine(String type) {
		mtype = type;
		mCommand = getCommand(type);
		mCacheStrategy = new WidgetCacheStrategy(type);
		mCacheExpired = mCacheStrategy.isNeedToDoNetWork();
		backupCache();
		setCacheStrategy(mCacheStrategy);
		mResultData = new WidgetResultData(getCommand());
	}

	public static void restoreCache(String cmd) {
		if (cmd == null
				|| (!cmd.equals(TYPE_TOOL) && !cmd.equals(TYPE_GAME) && !cmd
						.equals(TYPE_ENTERTAINMENT))) {
			LogUtil.d(TAG, "invalid cmd");
			return;
		}
		WidgetCacheStrategy strategy = new WidgetCacheStrategy(cmd);
		File f1 = strategy.getCacheFile();
		File f2 = strategy.getBakCacheFile();
		if (f1.exists() && f1.length() != f2.length()) {
			try {
				FileInputStream fis = new FileInputStream(f2);
				FileOutputStream fos = new FileOutputStream(f1);
				IOTools.copyStream(fis, fos);
				fis.close();
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void backupCache() {
		if (mCacheStrategy == null) {
			return;
		}
		File f1 = mCacheStrategy.getCacheFile();
		File f2 = mCacheStrategy.getBakCacheFile();
		if (f1.exists() && f1.length() != f2.length()) {
			try {
				FileInputStream fis = new FileInputStream(f1);
				FileOutputStream fos = new FileOutputStream(f2);
				IOTools.copyStream(fis, fos);
				fis.close();
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public boolean isCacheExpired() {
		return mCacheExpired;
	}

	private String getCommand(String type) {
        return "widget";
	}

	@Override
	public String getCommand() {
		return mCommand;
	}

	@Override
	protected void getParams(Context context, Map<String, String> custParams) {
		custParams.put("type", mtype);
		custParams.put("topId", String.valueOf(getGroup()));
	}

	private int getGroup() {
		WidgetResultData result = new WidgetResultData(mCommand);
		File cache = mCacheStrategy.getCacheFile();
		if (!cache.exists()) {
			return 0;
		}
		boolean ret = false;
		try {
			if (cache.length() > 1024 * 100 || cache.length() <= 0) {
				cache.delete();
			} else {
				FileInputStream fis = new FileInputStream(cache);
				byte[] bytes = new byte[(int) cache.length()];
				fis.read(bytes);
				fis.close();
				String content = new String(bytes, "UTF-8");
				ret = result.parseResult(content);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (!ret) {
			return 0;
		}
		return result.getGroup();
	}
}
